package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;

import commands.LoginCheckCommand;

/**
 * Panel for logging in to system
 * 
 * @author eks691
 */
public class LoginPanel extends JPanel {
	/** Field for user id */
	JTextField idField;

	/** Field for user password */
	JPasswordField passwordField;

	public static final int TEXTFIELD_LENGTH = 20;

	/**
	 * Login panel
	 */
	public LoginPanel() {
		setLayout(new BorderLayout());

		JPanel idPanel = new JPanel();
		idPanel.add(new JLabel("      User Id:"));
		idField = new JTextField(TEXTFIELD_LENGTH);
		idPanel.add(idField);
		add(idPanel, BorderLayout.PAGE_START);

		JPanel passwordPanel = new JPanel();
		passwordPanel.add(new JLabel("Password:"));
		passwordField = new JPasswordField(null, TEXTFIELD_LENGTH);
		passwordPanel.add(passwordField);
		add(passwordPanel, BorderLayout.CENTER);

		JPanel buttonsPanel = new JPanel();
		JButton loginButton = new JButton("Login");
		buttonsPanel.add(loginButton);
		JButton cancelButton = new JButton("Shutdown");
		buttonsPanel.add(cancelButton);
		JButton newUserButton = new JButton("New User");
		buttonsPanel.add(newUserButton);
		add(buttonsPanel, BorderLayout.PAGE_END);

		LoginListener loginListener = new LoginListener();
		loginButton.addActionListener(loginListener);

		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});

		newUserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				getTopLevelAncestor().setVisible(false);
				ApplicationFrame appFrame = new ApplicationFrame();
				appFrame.setVisible(true);
			}
		});
	}

	/**
	 * Class to listen for a button being pressed on login panel
	 */
	private class LoginListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent event) {
			String errorMessage = null;
			LoginCheckCommand.UserType userType = null;
			String idAsString = idField.getText(); // get login id from the
			// textbox
			int id = 0;
			if (idAsString != null && idAsString.length() > 0) {
				try {
					id = Integer.parseInt(idAsString); // user id's are ints
				} catch (NumberFormatException e) {
					errorMessage = "Please enter an integer value for the id";
				}
			} else
				errorMessage = "Must enter an integer for the id";
			if (errorMessage == null) {
				char[] password = passwordField.getPassword(); // passwords are
				// strings
				LoginCheckCommand loginCmd = new LoginCheckCommand();
				loginCmd.login(id, new String(password)); // this will log the
				// user in
				if (loginCmd.wasSuccessful()) {
					userType = loginCmd.getUserType();
					if (userType == LoginCheckCommand.UserType.STUDENT
							|| userType == LoginCheckCommand.UserType.EMPLOYEE
							|| userType == LoginCheckCommand.UserType.MANAGER) {
					} else
						errorMessage = "Must be a student, employee or manager";
				} else
					errorMessage = "Invalid user number or password";

			}
			if (errorMessage != null) {
				JOptionPane.showMessageDialog(getTopLevelAncestor(),
						errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
			} else {
				getTopLevelAncestor().setVisible(false);
				MainMenuFrame frame = new MainMenuFrame(userType, id);
				frame.setVisible(true);
			}
		}
	}

	public static final long serialVersionUID = 1;
}
